import { v3 } from 'cc';
import { screen } from 'cc';
import { view } from 'cc';
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BgAdapter')
export class BgAdapter extends Component {
    protected onLoad(): void {
        screen.on(`window-resize`, this.updateBg, this);
        this.updateBg();
    }

    protected onDestroy(): void {
        screen.off(`window-resize`, this.updateBg, this);
    }

    updateBg() {
        const visibleSise = view.getVisibleSize();
        const resolutionSize = view.getDesignResolutionSize();
        const ratio = visibleSise.width / visibleSise.height;
        const designRatio = resolutionSize.width / resolutionSize.height;
        if (ratio > designRatio) {
            const scale = visibleSise.width / resolutionSize.width;
            this.node.setScale(v3(scale, scale, 1))
        } else {
            const scale = visibleSise.height / resolutionSize.height;
            this.node.setScale(v3(scale, scale, 1))
        }
    }
}


